package cn.tedu.crm.business.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.tedu.crm.base.exception.ServiceException;
import cn.tedu.crm.business.mapper.AssessMapper;
import cn.tedu.crm.business.pojo.dto.AssessLoadParam;
import cn.tedu.crm.business.pojo.dto.AssessSaveParam;
import cn.tedu.crm.business.pojo.entity.Assess;
import cn.tedu.crm.business.pojo.vo.AssessVO;
import cn.tedu.crm.business.pojo.vo.DeptVO;
import cn.tedu.crm.business.pojo.vo.StaffVO;
import cn.tedu.crm.business.service.AssessService;
import cn.tedu.crm.orders.mapper.OrdersMapper;
import cn.tedu.crm.orders.pojo.dto.OrdersQueryByStaffId;
import cn.tedu.crm.orders.service.OrdersService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



@Service
@Slf4j
public class AssessServiceImpl implements AssessService {

    @Autowired
    private AssessMapper assessMapper;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<AssessVO> load(AssessLoadParam assessLoadParam) {
        Assess assess = new Assess();
        BeanUtils.copyProperties(assessLoadParam,assess);
        List<AssessVO> list = assessMapper.load(assess);
        for (AssessVO assessVO : list){
            String progress = getProgress(assessVO);
            assessVO.setProgress(progress);
            assessVO.setStaff(assessMapper.staffById(assessVO.getId()));
        }
        return list;
    }

    @Override
    public List<DeptVO> loadDept() {
        List<DeptVO> list =  assessMapper.loadDept();
        return list;
    }

    @Override
    public List<StaffVO> loadStaff(Long deptId) {
        List<StaffVO> list =  assessMapper.loadStaff(deptId);
        return list;
    }

    @Override
    public void save(AssessSaveParam assessSaveParam) {
        Assess assess = new Assess();
        BeanUtils.copyProperties(assessSaveParam,assess);
        assess.setStatus(1L);
        assess.setCreateTime(new Date());
        assess.setUpdateTime(new Date());
        log.debug(":{}",StpUtil.getLoginId());
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        assess.setCreateId(loginIdAsLong);
        assessMapper.insert(assess);
    }

    @Override
    public void delete(Long id) {
        log.debug("删除考核记录：{}", id);
        Assess assess = new Assess();
        assess.setId(id);
        assessMapper.deleteById(assess);
    }

    @Override
    public List<AssessVO> select(AssessLoadParam assessLoadParam) {
        Assess assess = new Assess();
        BeanUtils.copyProperties(assessLoadParam,assess);
        List<AssessVO> list = assessMapper.select(assess);
        for (AssessVO assessVO : list){
            String progress = getProgress(assessVO);
            assessVO.setProgress(progress);
            assessVO.setStaff(assessMapper.staffById(assessVO.getId()));
        }
        return list;
    }

    private String getProgress(AssessVO assessVO){
        Long staffId = assessVO.getStaffId();
        Date createTime = assessVO.getCreateTime();
        Date assess = assessVO.getAssess();
        OrdersQueryByStaffId ordersQueryByStaffId = new OrdersQueryByStaffId();
        ordersQueryByStaffId.setStaffId(staffId);
        ordersQueryByStaffId.setCreateTime(createTime);
        ordersQueryByStaffId.setAssess(assess);
        Long cost = ordersMapper.costByStaffId(ordersQueryByStaffId);
        Long user = ordersMapper.userByStaffId(ordersQueryByStaffId);
        Long l = ordersService.loadByStaffId(staffId, createTime, assess);
        Double number = new Double(l);
        Double costN = new Double(cost);
        Double userN = new Double(user);
        Double orderNumber = assessVO.getOrderIndex();
        Double costNumber = assessVO.getCostIndex();
        Double userNumber = assessVO.getUserIndex();

        Double num =0.0;
        String s = "";

        if ( orderNumber != null  && orderNumber !=0) {
            num = number / orderNumber;
            s = String.format("%.0f%%", num * 100);
        }

        if ( userNumber != null  && userNumber !=0) {
            num = userN / userNumber;
            s = String.format("%.0f%%", num * 100);
        }

        if ( costNumber != null  && costNumber !=0) {
            num = costN / costNumber;
            s = String.format("%.0f%%", num * 100);
        }
        return s;

    }

}




